/*b9f/v8OT6j8EFfl+iOkqV15DXUsf4RiL7cXFpKivDbm1kHMgaNoWSABshWpJ4IqV*/
import constants from '../../constants';
import ampub from 'ampub';

const { components: { LoginContext: { getContext } }, utils: { listUtils: { listConst: { QUERYINFOKEY } } } } = ampub;

const { BILL_SOURCE_TYPES, LIST_BTNS: { HEAD }, AREA: { LIST: { GRID_AREA: tableId } } } = constants;

export default function rowSelected(props, moduleId, record, index, status) {
	setBatchBtnsEnable.call(this, props, moduleId);
}

/**
 *
 * 列表设置批量按钮的可用性
 *
 * @param {*} props
 * @param {*} moduleId
 */
function setBatchBtnsEnable(props, moduleId) {
	const { button } = props;

	//是否显示【刷新】按钮
	let isQueryed = getContext(QUERYINFOKEY.isQueryed, QUERYINFOKEY.dataSource);
	let queryInfo = getContext(QUERYINFOKEY.queryInfo, QUERYINFOKEY.dataSource);
	if (isQueryed && queryInfo) {
		button.setButtonDisabled('Refresh', false);
	} else {
		button.setButtonDisabled('Refresh', true);
	}

	setLinkStatus(props);
}

/**
 * 设置联查按钮状态
 */
function setLinkStatus(props) {
	const { table, button } = props;
	let checkedRows = table.getCheckedRows(tableId);
	let batchBtns = Object.values(HEAD);
	if (checkedRows.length == 0) {
		button.setButtonDisabled(batchBtns, true);
		return;
	}

    // 优先显示表头所有按钮，下面逻辑再进行处理置灰
    button.setButtonDisabled(batchBtns, false);

	/* 默认取第一条 */
	//联查 设备卡片按钮
	/* 减少态或未关联设备卡片禁用 联查设备卡片按钮 */
	let [
		{
			data: {
				values: {
					// 关联设备主键
					pk_equip: { value: pk_equip },
					// 资产状态
					asset_state: { value: asset_state },
					// 来源单据类型
					bill_source: { value: bill_source },
					// 来源单据pk
					pk_bill_src: { value: pk_src },
					// 来源单据交易类型
					transi_type_source: { value: transi_type_source }
				}
			}
		}
	] = checkedRows;

	let equip_flag = !!pk_equip && 'exist' == asset_state;
	button.setButtonDisabled(HEAD.QueryAboutCard, !equip_flag);

	//获取卡片来源交易类型 有默认值handin
	if (transi_type_source === BILL_SOURCE_TYPES.handin_src) {
		transi_type_source = null;
	}
	// 获取卡片来源单据类型
	let type_src = transi_type_source || bill_source;

	if (type_src && pk_src && !type_src.startsWith(BILL_SOURCE_TYPES.equip_src)) {
		button.setButtonDisabled(HEAD.QueryAboutBusiness, false); //  联查 单据追溯
	} else {
		button.setButtonDisabled(HEAD.QueryAboutBusiness, true); //  联查 单据追溯
	}
    
}

export { setBatchBtnsEnable };

/*b9f/v8OT6j8EFfl+iOkqV15DXUsf4RiL7cXFpKivDbm1kHMgaNoWSABshWpJ4IqV*/